package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 华商网
 * @author Administrator
 * abc
 */
public class HswNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://news.hsw.cn/system/[0-9]{4}/[0-9]{4}/[0-9]{5,}.shtml",
				"http://it.hsw.cn/[0-9]{6}/[0-9]{12}b.shtml",
				"http://finance.hsw.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{9}.shtml",
				"http://news.hsw.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{9}.shtml"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		if (url.startsWith("http://finance.hsw.cn/")) {
			title=doc.select("td.bt1").text();
			if (StringUtils.isBlank(title)){
				title=doc.select("h1").text();
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("#endText"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.text"));
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select(".link_g_14 p"));
			}
			date=DateUtils.matchDate(doc.select("div.endContent > div.main > div.hd >div.titBar").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div.origin.f12").text());
			}
		}else if(url.startsWith("http://news.hsw.cn/system/")){
			title=doc.select("h1.headline").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("h1").text();
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div.contents"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.photoarea"));
			}
			date=DateUtils.matchDate(doc.select("div.origin").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("p.origin").text());
			}
		}else if(url.startsWith("http://it.hsw.cn/")){
			title=doc.select("div.nr_cen > div.nry_left > h1.nry_wzb").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.nr_cen > div.nry_left > div.nry_zw"));
			dateStr=doc.select("div.nr_cen > div.nry_left > div.nry_wxcc >span").text();
			date=DateUtils.matchDate(dateStr);
		}
		if (date==null){
			date=DateUtils.matchDate(url);
		}
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			String author=doc.select("div.news_main div.row div.origin span a").text();
			if(StringUtils.isBlank(author)){
				author=JSoupUtils.matchAuthor(doc, "来源:");
			}
			news.setAuthor(author);
			return news;

	}
	public static void main(String[] args) {
		HswNewsAnalyse hsw=new HswNewsAnalyse();
		String url="http://finance.hsw.cn/system/2010/07/30/050582130.shtml";
//		String url="http://it.hsw.cn/071378/346676513197b.shtml";
//		String url="http://finance.hsw.cn/system/2012/11/22/051541613.shtml";
		System.out.println(hsw.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);

		NewsMeta parserHtml=hsw.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
